package com.sumainfor.pc.epc.controller.market;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sumainfor.common.exception.TCException;
import com.sumainfor.common.util.DateUtils;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.pc.epc.feign.GetUserInfoFeign;
import com.sumainfor.pc.epc.service.impl.GmfProviderService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Author Eddy·Chen
 * @Email 835033913@qq.com
 * @Create 2019-05-17 13:56
 * 总经办查询
 */
@RestController
@RequestMapping("/provider/key/gmf")
public class GmfProviderController {

    @Autowired
    private GmfProviderService service;

    @Autowired
    private GetUserInfoFeign userInfoFeign;

    /**
     * 查询装修  计划数、套数、成本、已付、待付
     *
     * @param miId
     * @param startTime
     * @param endTime
     * @return
     */
    @PostMapping("/getDecoratePlanTableCount")
    public Map<String, Object> getDecoratePlanTableCount(Integer miId, String startTime, String endTime) {
        if (miId == null) {
            throw new TCException("参数不可为空");
        }
        return service.getDecoratePlanTableCount(miId, startTime, endTime);
    }

    /**
     * 查询子表格数据 装修计划
     *
     * @param size
     * @param current
     * @param miId
     * @param startTime
     * @param endTime
     * @return
     */
    @PostMapping("/selectPlanForSubTable")
    public IPage<Map<String, Object>> selectPlanForSubTable(Long size, Long current, Integer miId, String startTime, String endTime) {
        if (miId == null) {
            throw new TCException("参数不可为空");
        }
        Page page = new Page(current, size);
        IPage<Map<String, Object>> mapIPage = service.selectPlanForSubTable(page, miId, startTime, endTime);
        for (Map<String, Object> map : mapIPage.getRecords()) {
            String userId = MapUtils.getString(map, "SysUserId");
            //收房专员名称查询
            JsonResult<List<Map<String, Object>>> result = userInfoFeign.getUserInfo(Arrays.asList(userId));
            String uName = "";
            if (result.isSuccess() && CollectionUtils.isNotEmpty(result.getResult())) {
                uName = (String) result.getResult().get(0).get("username");
            }
            map.put("InUserName", uName);
            DateUtils.formatDateByMap(map);
        }
        return mapIPage;
    }

    /**
     * 查询子表格 装修套数
     *
     * @param size
     * @param current
     * @param miId
     * @param startTime
     * @param endTime
     * @return
     */
    @PostMapping("/selectHousingForSubTable")
    public IPage<Map<String, Object>> selectHousingForSubTable(Long size, Long current, Integer miId, String startTime, String endTime) {
        if (miId == null) {
            throw new TCException("参数不可为空");
        }
        Page page = new Page(current, size);
        return service.selectHousingForSubTable(page, miId, startTime, endTime);
    }

    /**
     * 查询装修 所需价格
     *
     * @param size
     * @param current
     * @param miId
     * @param startTime
     * @param endTime
     * @return
     */
    @PostMapping("/selectCostForSubTable")
    public IPage<Map<String, Object>> selectCostForSubTable(Long size, Long current, Integer miId, String startTime, String endTime) {
        if (miId == null) {
            throw new TCException("参数不可为空");
        }
        Page page = new Page(current, size);
        return service.selectCostForSubTable(page, miId, startTime, endTime);
    }

    /**
     * 查询装修计划房源信息
     *
     * @param params
     * @return
     */
    @PostMapping("/selectPlanHousingTableForAll")
    public JsonResult selectPlanHousingTableForAll(@RequestParam Map<String, Object> params) {
        return service.selectPlanHousingTableForAll(params);
    }

}
